package com.yenlien.traceyun.plant.controller;

import com.yenlien.traceyun.business.entity.Detection;
import com.yenlien.traceyun.plant.service.DetectionService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 检测
 *
 * @author maybe
 * @date 2020年7月29日
 */
@RestController
@RequestMapping("/detection")
public class DetectionController extends BaseController {
    @Autowired
    private DetectionService detectionService;

    //检测
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(Detection detection, HttpServletRequest request) throws Exception {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        detectionService.save(detection, files);
        return success("success");
    }

    //根据关联ID查询详情
    @RequestMapping(value = "/findByRelevanceId/{relevanceId}", method = RequestMethod.GET)
    public Object findByRelevanceId(@PathVariable("relevanceId") String relevanceId) {
        return success(detectionService.findByRelevanceId(relevanceId));
    }

    //根据ID查看详情
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Object findById(@PathVariable("id") String id) {
        return success(detectionService.findById(id));
    }
}
